let utils = require('../../../../../utils/util.js');
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		showNewTissue: false,
		hasVal: true,
		tempFilePaths: '', //临时头像
		typeId: '', //组织id
		typeName: '', //组织名称
		name: '', //新建名称
		tissueId: 0, //当前单位id，一级单位值默认为: 0
		parentId: 0, //当前级目录id
		fatherParentId: 0, //上一级目录id
		tissueList: [], //单位数据
		teamList: [], //团队信息
		tissueNames: [], //当前单位名称
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		const {
			typeId,
			parentId,
			fatherParentId
		} = options;
		if (typeId) {
			this.setData({
				typeId
			})
			switch (parseInt(typeId)) {
				case 1:
					this.setData({
						typeName: '学校'
					})
					break;
				case 2:
					this.setData({
						typeName: '企业'
					})
					break;
				case 3:
					this.setData({
						typeName: '社团'
					})
					break;
				case 4:
					this.setData({
						typeName: '其他'
					})
					break;
				default:
					break;
			}
		}
		if(fatherParentId){
			this.setData({
				fatherParentId
			})
		}
		var currentId = fatherParentId ? fatherParentId : parentId
		this.tissuelist(currentId)
	},
	onUnload: function() {
		console.log('onUnload');
    wx.redirectTo({
      url: '/pages/admin/team/manage/index/index'
    })
		// var fatherParentId = this.data.fatherParentId
		// console.log(fatherParentId);
		// var typeId = this.data.typeId
		// if (fatherParentId != 0) {
    //   wx.redirectTo({
		// 		url: '/pages/admin/team/manage/relation_sub/relation_sub?fatherParentId=' + fatherParentId + '&typeId=' +
		// 			typeId
		// 	})
		// }

		// var pages = getCurrentPages();
		// console.log(pages.length);

	},
	bindKeyInput(e) {
		this.setData({
			name: e.detail.value
		})
	},
	// 创建组织单位
	newTissue() {
		var data = {
			tissueId: this.data.tissueId,
			name: this.data.name,
			headerUrl: this.data.tempFilePaths,
			tissueType: this.data.typeId
		}
		if (!this.data.name) {
			wx.showToast({
				title: '请输入单位名称',
				icon: 'none',
				duration: 2000
			})
			return
		} else if (!this.data.tempFilePaths) {
			wx.showToast({
				title: '请上传单位头像',
				icon: 'none',
				duration: 2000
			})
			return
		}
		utils.requestTeam(utils.teamApi + '/team/save/tissue', 'POST', data, 'application/x-www-form-urlencoded')
			.then(res => {
				if (res.code == 0) {
					wx.showToast({
						title: '创建成功',
						icon: 'success',
						duration: 2000
					})
					this.tissuelist()
					setTimeout(() => {
						this.cancleHandle(this.data.tissueId)
					}, 2100)
				}
			})
	},
	cancleHandle() {
		this.setData({
			showNewTissue: false
		})
	},
	showNewTissueHandle() {
		this.setData({
			showNewTissue: true
		})
	},
	// 选择图片
	chooseImage() {
		var _this = this
		wx.chooseImage({
			count: 1,
			sizeType: ['original', 'compressed'],
			sourceType: ['album', 'camera'],
			success(res) {
				const tempFilePaths = res.tempFilePaths
				_this.setData({
					tempFilePaths: tempFilePaths
				})
				wx.uploadFile({
					url: utils.teamApi + '/team/save/teamshowimg',
					header: {
						'cookie': "_auth=" + wx.getStorageSync('_auth_team'),
						"Content-Type": "multipart/form-data"
					},
					filePath: tempFilePaths[0],
					name: 'files',
					formData: {
						flag: 5
					},
					success(res) {
						const data = JSON.parse(res.data)
						if (data.code == 0) {
							_this.setData({
								tempFilePaths: data.data[0]
							})
						}
					}
				})
			}
		})
	},
	// 查询当前单位节点下全部的单位和团队
	tissuelist(id = 0) {
		var data = {
			id: id || 0,
			typeId: this.data.typeId
		}
		this.setData({
			tissueId: id || 0
		})
		this.tissue(id)
		utils.requestTeam(utils.teamApi + '/team/find/tissuelist', 'POST', data, 'application/x-www-form-urlencoded')
			.then(res => {
				if (res.code == 0) {
					const {
						tissueList,
						teamList,
						tissueNames
					} = res.data
					if (tissueList.length == 0) {
						this.setData({
							tissueList,
							teamList,
							tissueNames,
							hasVal: false
						})
						return;
					}
					this.setData({
						tissueList,
						teamList,
						tissueNames,
						parentId: tissueList[0].parentId,
						hasVal: true
					})
					
				}
			})
	},
	//查询父级单位
	tissue(parentId) {
		var data = {
			parentId
		}
		utils.requestTeam(utils.teamApi + '/team/get/tissue', 'POST', data, 'application/x-www-form-urlencoded')
			.then(res => {
				console.log(res);
				if (res.code == 0) {
					console.log('上一级id',res.data.parentId);
					this.setData({
						fatherParentId: res.data.parentId
					})
				} else {
// 					this.setData({
// 						fatherParentId: 0
// 					})
				}
			})
	},
	//点击单位进入下一级
	toSub(e) {
		const {
			id,
			tissuename
		} = e.currentTarget.dataset
		this.setData({
			tissuename
		})
		this.tissuelist(id)
	},
	// 从属
	tissuerelate(){
		var data = {
			tissueId:this.data.tissueId
		}
		utils.requestTeam(utils.teamApi + '/team/save/tissuerelate', 'POST', data, 'application/x-www-form-urlencoded')
			.then(res => {
				console.log(res);
				if (res.code == 0) {
					wx.showToast({
						title: '已从属当前单位',
						icon: 'none',
						duration: 2000
					})
					this.tissuelist(this.data.tissueId)
				}else if(res.code == 312){
					wx.showToast({
						title: '已从属当前单位',
						icon: 'none',
						duration: 2000
					})
				}else{
					wx.showToast({
						title: '不可从属当前单位',
						icon: 'none',
						duration: 2000
					})
				}
			})
	},
	 toNavigator(e){
		var { url } = e.currentTarget.dataset
		utils.navigateTo({
			url: url
		})
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},


	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
